from pydantic import BaseModel, validator
from typing import Optional, List
from datetime import datetime


# 用户相关模型
class UserBase(BaseModel):
    user_name: str
    user_phone: Optional[str] = None
    address: Optional[str] = None  # 添加地址字段


class UserCreate(UserBase):
    user_password: str

    @validator('user_password')
    def password_strength(cls, v):
        if len(v) < 6:
            raise ValueError('密码长度至少6位')
        return v


class UserLogin(BaseModel):
    user_name: str
    user_password: str


class UserUpdate(BaseModel):
    user_name: Optional[str] = None
    user_password: Optional[str] = None
    user_phone: Optional[str] = None
    address: Optional[str] = None  # 添加地址字段


class UserResponse(UserBase):
    user_id: int
    avatar_url: Optional[str] = None  # 添加头像URL字段
    created_at: datetime
    updated_at: Optional[datetime] = None  # 添加更新时间字段

    class Config:
        from_attributes = True


# 地址管理模型
class AddressCreate(BaseModel):
    contact_name: str
    contact_phone: str
    address_detail: str
    is_default: bool = False


class AddressUpdate(AddressCreate):
    pass  # 字段与 AddressCreate 完全一致，无需重复定义


# 密码重置相关模型
class PasswordResetRequest(BaseModel):
    old_password: str
    new_password: str

    @validator('new_password')
    def new_password_strength(cls, v):
        if len(v) < 6:
            raise ValueError('新密码长度至少6位')
        return v

    class Config:
        from_attributes = True


class ForgotPasswordRequest(BaseModel):
    user_name: str
    user_phone: str
    new_password: str

    class Config:
        from_attributes = True


class ForgotPasswordResponse(BaseModel):
    message: str
    user_id: int
    user_name: str
    next_step: str

    class Config:
        from_attributes = True


# 商品相关模型
class GoodsBase(BaseModel):
    good_name: str
    good_number: int
    good_sell_num: int
    good_positive_review_rate: float
    # good_purchase_price: float
    good_sell_price: float
    good_description: Optional[str] = None


class GoodsCreate(GoodsBase):
    good_id: int


class GoodsResponse(GoodsBase):
    good_id: int

    class Config:
        from_attributes = True


class GoodsSearch(BaseModel):
    keyword: Optional[str] = None
    good_id: Optional[int] = None


# 商品分页和搜索响应模型
class PaginatedGoodsResponse(BaseModel):
    goods: List[GoodsResponse]
    pagination: dict

    class Config:
        from_attributes = True


class SearchGoodsResponse(BaseModel):
    goods: List[GoodsResponse]
    total: int
    filters: dict

    class Config:
        from_attributes = True


# 订单相关模型
class OrderBase(BaseModel):
    good_id: int
    order_number: int


class OrderCreate(OrderBase):
    pass


class OrderUpdate(OrderBase):
    pass


class OrderResponse(BaseModel):
    order_id: int
    user_id: Optional[int] = None
    user_name: str
    user_phone: Optional[str] = None
    address: Optional[str] = None  # 添加地址字段
    good_id: int
    good_name: str
    good_price: Optional[float] = None
    order_number: int
    total_price: Optional[float] = None
    order_profit: float
    order_time: str
    created_at: Optional[str] = None
    updated_at: Optional[str] = None

    class Config:
        from_attributes = True


class OrderListResponse(BaseModel):
    orders: List[OrderResponse]
    total: int
    limit: int
    offset: int = 0
    user_name: str
    message: Optional[str] = None

    class Config:
        from_attributes = True


# 订单分页响应模型（为未来扩展准备）
class PaginatedOrderResponse(BaseModel):
    orders: List[OrderResponse]
    total: int
    total_profit: float
    pagination: dict
    user_name: str

    class Config:
        from_attributes = True


# 收藏相关模型
class FavoriteBase(BaseModel):
    good_id: int


class FavoriteCreate(FavoriteBase):
    pass


class FavoriteResponse(BaseModel):
    user_id: int
    good_id: int
    good_name: str
    created_at: datetime
    good_sell_price: Optional[float] = None
    good_description: Optional[str] = None
    good_positive_review_rate: Optional[float] = None

    class Config:
        from_attributes = True


# 收藏分页响应模型（为未来扩展准备）
class PaginatedFavoriteResponse(BaseModel):
    items: List[FavoriteResponse]
    total: int
    page: int
    page_size: int
    has_more: bool

    class Config:
        from_attributes = True


# 智能咨询模型
class ChatRequest(BaseModel):
    question: str
    session_id: Optional[str] = None  # 已移除 good_id 和 good_name


class ChatResponse(BaseModel):
    response: str


class ChatHistorySave(BaseModel):
    user_id: int
    session_id: str
    role: str
    message_type: str
    content: str
    image_url: Optional[str] = None
    good_id: Optional[int] = None


class ChatHistoryResponse(BaseModel):
    id: int
    session_id: str
    role: str
    message_type: str
    content: str
    image_url: Optional[str] = None
    good_id: Optional[int] = None
    created_at: datetime

    class Config:
        from_attributes = True


class ChatSessionResponse(BaseModel):
    session_id: str
    last_time: datetime
    message_count: int

    class Config:
        from_attributes = True


class ChatHistoryListResponse(BaseModel):
    history: List[ChatHistoryResponse]

    class Config:
        from_attributes = True


class ChatSessionListResponse(BaseModel):
    sessions: List[ChatSessionResponse]

    class Config:
        from_attributes = True


# Token模型
class Token(BaseModel):
    access_token: str
    token_type: str
    user_id: int
    user_name: str


class TokenData(BaseModel):
    user_id: Optional[int] = None
    user_name: Optional[str] = None


# 通用响应模型
class MessageResponse(BaseModel):
    message: str

    class Config:
        from_attributes = True


class SuccessResponse(BaseModel):
    success: bool
    message: str
    data: Optional[dict] = None

    class Config:
        from_attributes = True


class ImageUploadResponse(BaseModel):
    success: bool
    message: str
    session_id: Optional[str] = None
    filename: Optional[str] = None

    class Config:
        from_attributes = True


class APIKeyValidationResponse(BaseModel):
    api_key_valid: bool

    class Config:
        from_attributes = True


class OrderCreateWithAddress(OrderCreate):
    address: Optional[str] = None


class OrderUpdateWithAddress(OrderUpdate):
    address: Optional[str] = None